package com.luhe.musicscore.ui;

import com.luhe.musicscore.symbol.Symbol;
import com.luhe.musicscore.symbol.instrument.piano.Note;
import com.luhe.musicscore.symbol.instrument.piano.clef.TrebleClef;
import com.luhe.musicscore.symbol.instrument.piano.line.Line;
import com.luhe.musicscore.ui.factory.PopMenuFactory;
import com.luhe.musicscore.ui.factory.TableFactory;
import com.luhe.musicxml.v1.ScorePartwise;


import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/*
 * 界面启动入口
 * ScorePartwise xml树节点的编辑在界面上的动态刷新
 */
public class Run {
    //树形面板
    static JPanel panel1 = new JPanel();

    //乐谱绘制面板
    static JPanel panel2 = new JPanel();

    // 属性编辑面板
    static JPanel panel3 = new JPanel();

    //scorepartwise树形菜单
    static JTree jTree;

    //反射获取node的jTree节点
    static DefaultMutableTreeNode createFromNode() {

        return null;
    }


    //乐谱绘制方法
    public static void paint(Graphics graphics) {

        int d = 20;
        if (Symbol.draw_border == true) {
            for (int i = 0; i <= 100; i = i + 1) {
                graphics.setColor(new Color(220, 220, 220));
                graphics.drawLine(0, d * i, 500, d * i);
                graphics.drawLine(d * i, 0, d * i, 500);
            }
        }


        TrebleClef trebleClef = new TrebleClef();
        trebleClef.x = 0;
        trebleClef.y = 0;
        trebleClef.paint(graphics);


        Line line = new Line();
        line.x = 40;
        line.y = 0;
        line.paint(graphics);


        Note note = new Note();
        note.x = 40;
        note.y = 40;
        note.paint(graphics);
    }

    // 启动方法
    public static void main(String[] args) throws IllegalAccessException {

        //正在编辑的乐谱
        ScorePartwise scorePartwise = new ScorePartwise();

        //窗体
        JFrame jFrame = new JFrame("五线谱绘制");
        jFrame.setLayout(null);
        jFrame.setTitle("五线谱编辑器");
        jFrame.setBounds(0, 0, 1200, 600);

        //菜单
        JMenuBar bar = new JMenuBar();
        JMenu menu = new JMenu("文件");
        JMenuItem item1 = new JMenuItem("打开");
        JMenuItem item2 = new JMenuItem("保存");
        bar.add(menu);
        menu.add(item1);
        menu.add(item2);
        jFrame.setJMenuBar(bar);


        //面板
        panel1.setBackground(Color.CYAN);
        panel2.setBackground(Color.LIGHT_GRAY);
        panel3.setBackground(Color.orange);
        jFrame.setLayout(null);
        panel1.setLayout(null);
        panel2.setLayout(null);
        panel3.setLayout(null);
        jFrame.add(panel1);
        jFrame.add(panel2);
        jFrame.add(panel3);


        //顶层树
      //  jTree = new JTree(createFromNode(scorePartwise));
      //  jTree.setBounds(0, 0, 200, jFrame.getHeight());
      //  panel1.add(jTree);


        //加入属性编辑表格
      //  panel3.add(new TableFactory().createTableFromNode(scorePartwise));

        // 显示
        jFrame.setVisible(true);

        //绘图线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(100);
                        paint(panel2.getGraphics());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }
        }).start();


        //面板自适应布局
        jFrame.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                Dimension size = e.getComponent().getSize();
                int height = (int) size.getHeight();
                int width = (int) size.getWidth();
                panel1.setBounds(0, 0, 200, (int) size.getHeight());
                panel2.setBounds(400, 0, width - 400, height);
                panel3.setBounds(200, 0, 200, height);
                jTree.setBounds(0, 0, 200, height);

            }
        });

        //树节点被单击：显示对应的属性编辑列表
        jTree.addTreeSelectionListener(new TreeSelectionListener() {
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                TreePath path = e.getNewLeadSelectionPath();
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
                Object nodeInfo = node.getUserObject();
                UserObject userObject = (UserObject) nodeInfo;
                panel3.removeAll();
               // panel3.add(new TableFactory().createTableFromNode(userObject.getNode()));
              //  System.out.println("选择节点:" + userObject.getName());

            }
        });

        //右键单击：显示要添加的字节点弹出列表
        jTree.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if (SwingUtilities.isRightMouseButton(e)) {
                    TreePath path = jTree.getPathForLocation(e.getX(), e.getY());
                    Rectangle pathBounds = jTree.getUI().getPathBounds(jTree, path);
                    if (pathBounds != null && pathBounds.contains(e.getX(), e.getY())) {
                        //弹出右键菜单
                        DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) jTree.getLastSelectedPathComponent();
                        System.out.println("treeNode" + treeNode);
                        JPopupMenu menu = new PopMenuFactory().createPopupMenuFromNode(treeNode);
                        menu.show(jTree, pathBounds.x, pathBounds.y + pathBounds.height);
                    }
                }
            }
        });


    }
}
